package com.example.pinkcat.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.pinkcat.R;
import com.example.pinkcat.activity.api.getData.GetData;

import java.util.Objects;

public class ChangePasswordPage extends AppCompatActivity {
    Context context=this;

    EditText edit_account;
    EditText edit_password1;
    EditText verification;
    TextView getVerification_textView;
    TextView btn_changePassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Objects.requireNonNull(getSupportActionBar()).hide();//去掉标题栏
        //        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        setContentView(R.layout.activity_change_password_page);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }//深色信息栏

        //
        edit_account=findViewById(R.id.edit_account);
        edit_password1=findViewById(R.id.edit_password1);
        verification=findViewById(R.id.edit_verification);
        getVerification_textView=findViewById(R.id.get_verification);
        btn_changePassword=findViewById(R.id.btn_changePassword);

        //监听
        listener();
    }

    Handler handler=new Handler(new Handler.Callback() {
        @SuppressLint("SetTextI18n")
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what==250){
                getVerification_textView.setText(R.string.get_verification);
                getVerification_textView.setClickable(true);
            }
            else
                getVerification_textView.setText(msg.what+"s");
            return true;
        }
    });


    void listener(){

        //更改密码
        btn_changePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                findViewById(R.id.progressBar_wait).setVisibility(View.VISIBLE);
                if (edit_account.getText().toString().length()<2||edit_password1.getText().toString().length()<2 || verification.getText().toString().length()<2){
                    Toast.makeText(context,"请输入必要的信息",Toast.LENGTH_SHORT).show();
                    findViewById(R.id.progressBar_wait).setVisibility(View.INVISIBLE);
                    return;
                }
                if (edit_account.getText().toString().length()==11){
                    if (GetData.changePassword(edit_account.getText().toString(),verification.getText().toString(),edit_password1.getText().toString(),context))
                        onBackPressed();
                }
                findViewById(R.id.progressBar_wait).setVisibility(View.INVISIBLE);
            }
        });


        //获取验证码监听并进入倒计时
        getVerification_textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (edit_account.getText().toString().length()<2){
                    Toast.makeText(context,"请输入正确的账号信息",Toast.LENGTH_SHORT).show();
                    return;
                }
                GetData.getVerification(edit_account.getText().toString(),edit_account.getText().toString(),context);
                new Thread(new Runnable() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void run() {
                        getVerification_textView.setClickable(false);
                        getVerification_textView.setTextColor(getResources().getColor(R.color.black_overlay));
                        int count=60;
                        while (count>0){
                            try {
                                Message message = new Message();
                                message.what = count;
                                handler.sendMessage(message);
                                Thread.sleep(1000);
                                count--;
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        Message message = new Message();
                        message.what = 250;
                        handler.sendMessage(message);
                        getVerification_textView.setTextColor(getResources().getColor(R.color.pink));
                    }
                }).start();
            }
        });

        //返回监听
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        //用户协议
        findViewById(R.id.useragreement).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context,PinCatAgreement.class);
                intent.putExtra("agreementTitle","用户协议");
                startActivity(intent);
            }
        });
        //隐私协议
        findViewById(R.id.privacy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context,PinCatAgreement.class);
                intent.putExtra("agreementTitle","隐私协议");
                startActivity(intent);
            }
        });

        //点击输入账号监听
        ImageView loginImage=findViewById(R.id.loginimage);
        edit_password1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                loginImage.setImageResource(R.mipmap.close_eyes);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        verification.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                loginImage.setImageResource(R.mipmap.close_eyes);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        edit_account.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                loginImage.setImageResource(R.mipmap.open_eyes);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }
}